package org.bmi.gwt.examples.clienttr.server;

import org.apache.log4j.Logger;
import org.bmi.gwt.examples.clienttr.shared.Element1Proxy;
import org.bmi.gwt.examples.clienttr.shared.Element2Proxy;
import org.bmi.gwt.mi.server.annotation.QueueHandler;
import org.bmi.gwt.mi.server.events.QueueEvent;
import org.bmi.gwt.mi.server.events.QueueEventHandler;
import org.bmi.gwt.mi.shared.Channel;
import org.bmi.gwt.mi.shared.MiTransaction;
import org.bmi.gwt.mi.shared.exceptions.InvalidArgumentException;

@QueueHandler(queueExpression = "org.bmi.gwt.mi.test.ClientTransaction")
public class ClientQueueEventHandler implements QueueEventHandler
{
	static protected Logger logger = Logger.getLogger(ClientQueueEventHandler.class);

	@Override
	public void onCreate(QueueEvent event)
	{}

	@Override
	public void onDispose(QueueEvent event)
	{}

	@Override
	public void onUpdate(QueueEvent event)
	{}

	@Override
	public void onBound(QueueEvent event)
	{
		Channel source = event.getSourceQueueChannel();
		logger.info("Created queue: " + source.getExpression());

		MiTransaction t = source.createTransaction();

		try
		{
			Element1Proxy proxy1 = t.proxyToNewObject(Element1Proxy.class);
			Element1Proxy proxy2 = t.proxyToNewObject(Element1Proxy.class, "String from server");

			Element2Proxy proxy3 = t.proxyToNewObject(Element2Proxy.class, proxy1.getCopy());
			Element2Proxy proxy4 = t.proxyToNewObject(Element2Proxy.class, proxy2);

			proxy3.getElement().print();
			proxy4.getElement().print();
		}
		catch (InvalidArgumentException e)
		{
			e.printStackTrace();
		}

		t.commit();
	}
}
